#!/usr/bin/python# 
# -*- coding: utf-8 -*-
# Copyright (c) tsm
# Author: tsm

# define global variables

import sys
import os
import shutil
import re

fileNames = {
  "rename": [
    "PROJECT_NAME.xcodeproj"
  ],
  "remove": [
    "PROJECT_NAME.xcodeproj/project.xcworkspace",
    "PROJECT_NAME.xcodeproj/xcuserdata"
  ],
  "replace_package_name": [
    "Info.plist"
  ],
  "replace_project_name": [
    "Info.plist",
    "PROJECT_NAME.xcodeproj/project.pbxproj"
  ]
}
                                 

def replaceString(filepath, src_string, dst_string):
    content = ""
    f1 = open(filepath, "rb")
    for line in f1:
        if src_string in line:
            content += line.replace(src_string, dst_string)
        else:
            content += line
    f1.close()
    f2 = open(filepath, "wb")
    f2.write(content)
    f2.close()
# end of replaceString

()

def replaceFileContent(path, pattern, replace):
  rin = open(path, 'r')
  text = rin.read()
  rin.close()
  text = re.sub(pattern, replace, text)
  wout = open(path, 'w')
  wout.write(text)
  wout.close()

projectName = ""
currentDir = os.path.split(os.path.realpath(__file__))[0]
iosRootDir = currentDir + "/build2/jsb-link/frameworks/runtime-src/proj.ios_mac/"
def findoutXcodeProjPath():
  for root, dirs, files in os.walk(iosRootDir):
    for dir in dirs: 
      if dir.find('xcodeproj') >= 0:
        return os.path.join(root, dir)
  return ""

def findoutOldProjName():
  for root, dirs, files in os.walk(iosRootDir):
    for dir in dirs: 
      if dir.find('xcodeproj') >= 0:
        return dir.replace(".xcodeproj", "")
  return ""


def changeProjectName(iosRootDir, newProjName):
    #rename project dir
    oldProjName = findoutOldProjName()
    oldPath = os.path.join(iosRootDir, oldProjName + ".xcodeproj")

    # remove useless files and folders
    for i in range(0, len(fileNames["remove"])):
        dst = fileNames["remove"][i].replace("PROJECT_NAME", oldProjName)
        filepath = os.path.join(iosRootDir, dst)
        print "删除:" + filepath
        if (os.path.exists(filepath) == True):
            shutil.rmtree(filepath)
                  
    newPath = os.path.join(iosRootDir, newProjName + ".xcodeproj")
    if (os.path.exists(oldPath) == True):
      print "oldpath:" + oldPath
      print "newpath:" + newPath
      os.rename(oldPath, newPath)
      replaceFileContent(newPath+"/project.pbxproj", oldProjName, newProjName)
    # done!
    print "Change Project Name Done!"

def changeDisplayName(displayName):
  infoPlistPath = os.path.join(iosRootDir, "ios/Info.plist")
  replaceFileContent(infoPlistPath, r'CFBundleDisplayName</key>\n.*<string>.*</string>', 'CFBundleDisplayName</key>\n\t<string>' + displayName + "</string>")

def changeBundleID(bundleid):
  xcodeprojDirPath = findoutXcodeProjPath()
  projectfile = os.path.join(xcodeprojDirPath, "project.pbxproj")
  print ("projectfile:" + projectfile)
  replaceFileContent(projectfile, r'PRODUCT_BUNDLE_IDENTIFIER = .*;', 'PRODUCT_BUNDLE_IDENTIFIER = ' + bundleid + ";")

print "你需要准备 1.bundle id; 2.display name; 3.studio简码(如FFM FGM)"
bundleid = raw_input("请输入bundle id: ")
displayname = raw_input("请输入display name: ")
studioName = raw_input("请输入studio简码: ")
projName = studioName + "_" + displayname.replace(" ", "")
changeBundleID(bundleid)
changeProjectName(iosRootDir, projName)
changeDisplayName(displayname)